import { defineEventHandler, H3Event } from 'h3'
import { hash } from 'bcrypt'
import { prisma } from '../utils/prisma'

export default defineEventHandler(async (event: H3Event) => {
  const method = event.method
  
  // 获取用户列表
  if (method === 'GET') {
    const users = await prisma.user.findMany({
      select: {
        id: true,
        username: true,
        email: true,
        role: true,
        status: true,
        created_at: true
      }
    })
    return users
  }
  
  // 创建用户
  if (method === 'POST') {
    const body = await readBody(event)
    const hashedPassword = await hash(body.password, 10)
    
    const user = await prisma.user.create({
      data: {
        username: body.username,
        email: body.email,
        password_hash: hashedPassword,
        role: body.role || 'user'
      }
    })
    return user
  }
}) 